package com.shopping.controller;

import cn.hutool.json.JSONObject;
import com.shopping.bean.Administrator;
import com.shopping.service.IAdminService;
import com.shopping.util.LayData;
import com.shopping.util.Page;
import com.shopping.util.SYS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("admin")
public class AdminController {
    @Autowired
    IAdminService adminService;
    @RequestMapping("getadmininfo")
    @ResponseBody
    public LayData getLoginAdmin(HttpSession session){
        Administrator loginAdmin=(Administrator) session.getAttribute(SYS.SYSTEM_LOGIN_ADMIN);
        int id=loginAdmin.getAdminid();
        Administrator administrator=adminService.getAdminByID(id);
        return LayData.success(200,"",administrator);
    }
    @RequestMapping("list")
    @ResponseBody
    public LayData list(Page page,String text){
        List<Administrator> list=adminService.getAdminList(text,page);
        int count=adminService.getAdminCount(text);
        return new LayData(count,list);
    }
    @RequestMapping("edit")
    @ResponseBody
    public LayData editAdmin(HttpSession session,Administrator administrator){
        if (administrator.getAdminid()==0){
            Administrator loginAdmin=(Administrator)session
                    .getAttribute(SYS.SYSTEM_LOGIN_ADMIN);
            int id=loginAdmin.getAdminid();
            administrator.setAdminid(id);
        }
        adminService.editAdmin(administrator);
        return LayData.success(200,null);
    }
    @RequestMapping("delete")
    @ResponseBody
    public LayData deleteAdmin(@RequestParam("id") Integer adminid){
        int result=adminService.deleteAdmin(adminid);
        if (result>0){
            return LayData.success(200,"删除成功");
        }else {
            return LayData.success(400,"删除失败");
        }
    }



    @RequestMapping("register")
    @ResponseBody
    public LayData register(@RequestBody Map<String, Object> requestData) {
        JSONObject jsonObject = new JSONObject(requestData);
        JSONObject adminJson = jsonObject.getJSONObject("admin");
        String confirmadminpassword = jsonObject.getStr("confirmadminpassword");

        Administrator admin = new Administrator();
        admin.setAdminname(adminJson.getStr("adminname"));
        admin.setAdminpassword(adminJson.getStr("adminpassword"));
        admin.setImgurl(adminJson.getStr("imgurl"));

        // 验证格式是否正确
        boolean check = SYS.check("^\\w{6,16}$", admin.getAdminname(), admin.getAdminpassword(), confirmadminpassword);
        if (!check) {
            return LayData.success(400, "输入的内容格式不正确");
        }
        // 验证两次输入的密码是否一致
        if (!confirmadminpassword.equals(admin.getAdminpassword())) {
            return LayData.success(400, "两次输入的密码不一样");
        }
        // 验证管理员账号是否已存在
        Administrator adminData = adminService.getAdmin(admin);
        if (adminData != null) {
            return LayData.success(400, "管理员账号已存在");
        }
        // 对密码进行加密处理
        String hexpass = SYS.digest(admin.getAdminpassword());
        admin.setAdminpassword(hexpass);
        // 调用服务层的添加方法
        int count = adminService.add(admin);
        if (count > 0) {
            return LayData.success(200, "注册成功", admin);
        } else {
            return LayData.success(400, "注册失败");
        }
    }

}
